package com.cw.config;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyLog {

    private Logger logger = LoggerFactory.getLogger(MyLog.class);

    @Pointcut("execution(public * com.cw.service.*.*(..))")
    public void point() {}

    @Before("point()")
    public void before(JoinPoint joinPoint) {

        Object[] args = joinPoint.getArgs();

        Signature signature = joinPoint.getSignature();

        logger.info("进入方法: {}, 方法参数: {}", signature, args );
    }

    @AfterReturning(pointcut = "point()", returning = "obj")
    public void returnAfter(Object obj) throws Throwable {

        logger.info("方法执行成功,返回参数为: {}", JSONObject.toJSONString(obj));
    }

}
